package test.util;

class STest{
    Integer id = integer++;
    String string = id+"";
    
    static Integer integer = 0;

    @Override
    public String toString() {
        return "STest [id=" + id + ", string=" + string + "]";
    }
}

public class SystemArraycopy {
    static void testInteger2(){
        Integer i1 = new Integer(10000);
        Integer i2 = new Integer(20000);
        i2 = i1;    // 这里传递的不是引用，而是值，因此 i2 与 i1 指向的不是同一个地址
        System.out.println(i1);
        System.out.println(i2);
        i2 = -10000;
        System.out.println(i1);
        System.out.println(i2);
    }
    static void testObject2(){
        STest s1 = new STest();
        STest s2 = new STest();
        s2 = s1;    // 这里传递的是引用，即 s2 与 s1 其实指向同一个地址
        System.out.println(s1);
        System.out.println(s2);
        s2.id = -100;
        System.out.println(s1);
        System.out.println(s2);
    }
    
    static <T> void printArray(T [] a){
        for( T t: a )
            System.out.println(t+",");
        System.out.println("");
    }
    
    static void copySTest(){
        final Integer size = 5;
        STest []s1 = new STest[size];
        for(int i=0; i<size; i++)
            s1[i] = new STest();
        
        STest []s2 = new STest[size];
        System.arraycopy(s1, 0, s2, 0, size); // 证明这个也是浅复制,s2[0] 与 s1[0] 指向同一个对象
        s2[0].id = -1;
        printArray(s1);
        printArray(s2);
    }
    
    public static void main(String[] args) {
//        testInteger2();
//        testObject2();
        copySTest();
    }
}
//output:
//10000
//10000
//10000
//-10000
//STest [id=0, string=0]
//STest [id=0, string=0]
//STest [id=-100, string=0]
//STest [id=-100, string=0]
